/**
 * Created by shidaiapp on 2017/9/29.
 */
import actionUrls from "../../../../api/ApiUrls";
import ApiBase from "../../../../api/moudles/ApiBase";


/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.userName = '';
  tiyData.tParams.employeeId = '';
  tiyData.tParams.isRepeated = false;
  tiyData.tParams.orderBy = '';
  tiyData.tParams.selectedUsers= [];
  tiyData.tData.userEmployeeList.splice(0,tiyData.tData.userEmployeeList.length);
  tiyData.tData.loginTypeDict = self.loginTypeDict;
 };

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  let data = {"pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "userName": tiyData.tParams.userName,
    "employeeId": tiyData.tParams.employeeId,
    "getRepeated" : tiyData.tParams.isRepeated,
    "orderBy": tiyData.tParams.orderBy
  };
  self.$Spin.show();
  self.$apiUser.getUserEmployee(data).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.userEmployeeList.splice(0,tiyData.tData.userEmployeeList.length)
      let userPage = response.result;
      tiyData.tPage.total = userPage.total;
      tiyData.tPage.stepNumber = userPage.pageSize;
      userPage.list.forEach(
        function (item) {
          tiyData.tData.userEmployeeList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

export const userInfo = (self, tiyData,index) => {
  let userId = tiyData.tData.userEmployeeList[index].userId;

  self.$Spin.show();
  self.$apiUser.getUserInfo({
    "userId": userId,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let record = response.result;
      tiyData.tParams.showUserInfo=[];
      tiyData.tParams.showUserInfo.firstName=record.user.firstname;
      tiyData.tParams.showUserInfo.lastName=record.user.lastname;
      let way = '';
      self.loginTypeDict.forEach(function (item) {
        if (item.value == record.user.thirdParty) {
          way = item.name;
          return;
        }
      });
      tiyData.tParams.showUserInfo.thirdParty=way;

      let stateStr = '';
      self.$store.getters.dictionaries.userStateDict.forEach(function(item){
        if(item.value==record.user.state){
          stateStr = item.name;
          return;
        }
      });
      tiyData.tParams.showUserInfo.state=record.user.stateStr;
      tiyData.tParams.showUserInfo.country=record.user.countryName;
      tiyData.tParams.showUserInfo.phone=record.user.phone;
      tiyData.tParams.showUserInfo.isPushMessage=record.user.isPushMessage==1?true:false;
      tiyData.tParams.showUserInfo.isAdEmail=record.user.isAdEmail==1?true:false;

      if(record.extendInfo!=null){
        tiyData.tParams.showUserInfo.facebook=record.extendInfo.facebook;
        tiyData.tParams.showUserInfo.whatsapp=record.extendInfo.whatsapp;
        tiyData.tParams.showUserInfo.twitter=record.extendInfo.twitter;
        tiyData.tParams.showUserInfo.instagram=record.extendInfo.instagram;
        tiyData.tParams.showUserInfo.source=record.extendInfo.source;
        tiyData.tParams.showUserInfo.inviteCode=record.extendInfo.inviteCode;
        tiyData.tParams.showUserInfo.useContent=record.extendInfo.useContent;
      }

      tiyData.tStatus.modalUserInfo.isShow=true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });

}
